<?php
/****************************************
 *  Class Name : ST_PHPForm 1.0 		*	
 *  Author: Saeid Moradi 				*
 *  Email: saeid3(at)gmail.com			*
 *  Date: 19 June 2009					*
 ****************************************/

class ST_PHPForm  {

	var $strFileName = "";
	var $strIndent	 = "";
	var $arrErrMsgs	 = array();
	var $arrInfMsgs	 = array();
	var $arrPostData = array();
	var $blnLocator  = false;	
	
	function ST_PHPForm() {
		preg_match("/[^\/]*$/", $_SERVER['PHP_SELF'], $arrSub);
		$this->strFileName = $arrSub[0];
		if ($this->isPost()) {
			while (list($strKey, $strVal) = each($_POST)){
				if (is_array($strVal)){
					$this->arrPostData[$strKey] = $strVal;
				}else {
					$strVal = trim($strVal);
					$strVal = $this->StripSlashes($strVal);
					$this->arrPostData[$strKey] = $strVal;
				}
			}
		}
	}
	
	function Open($strId, $arrOtherAttributes = array(), $strEnctype = "multipart", $strAction = "", $strMethod = "post") {
		$strId = (($strId == "") ? "" : " id=\"".$strId."\"" );
		$strOtherAttributes = $this->_OtherAttributes($arrOtherAttributes);
		$strAction = (($strAction == "") ? $this->strFileName : $strAction);
		switch ($strMethod) {
			case "get":
				$strMethod = "get";
				break;
			case "post":
				$strMethod = "post";
				break;		
			default:
				$strMethod = "post";
				break;
		}
		switch ($strEnctype){
			case "text":
				$strEnctype = "text/plain";
				break;
			case "multipart":
				$strEnctype = "multipart/form-data";
				break;
			case "urlencoded":
				$strEnctype = "application/x-www-form-urlencoded";
				break;
			default:
				$strEnctype = "application/x-www-form-urlencoded";
				break;
		}
		return $this->strIndent."<form ".$strId." action=\"".$strAction."\" method=\"".$strMethod."\"".$strOtherAttributes." enctype=\"".$strEnctype."\">\r\n";		
	}
	
	function Close() {
		return $this->strIndent."</form>\r\n";
	}
	
	function TextBox($strId, $strLabel, $strValue, $arrOtherAttributes = array(), $strType = "text", $intMaxLength = 0, $intTabIndex = NULL, $blnWithLabel = false){
		$strMaxLength = (($intMaxLength > 0) ? " maxlength=\"".$intMaxLength."\"" : "");
		$strTabIndex = $this->_TabIndex($intTabIndex);
		$strValue = (($strType == "text") ? $this->_Value($strId, $strValue) : "");
		$strOtherAttributes = $this->_OtherAttributes($arrOtherAttributes);
		$strClassName = $this->_ClassName($strId);
		$strLocator = $this->_Locator($strId, $strClassName);
		$strField = "<input id=\"".$strId."\" name=\"".$strId."\" type=\"".$strType."\" value=\"".$strValue."\" class=\"".$strClassName."\"".$strOtherAttributes.$strMaxLength.$strTabIndex.$strLocator." />";
		$blnWithLabel = ($strLabel == "") ? false : true;
		return $this->_FormHtmlElement($strLabel,$strField, $strId, $blnWithLabel);
	}
	
	function Label($strId, $strLabel, $arrOtherAttributes = array()){
		$strField = "<label for=\"".$strId."\">".$strLabel."</label>";
		return $this->_FormHtmlElement($strLabel, $strField,$strId, false);
	}
	
	function HiddenField($strId, $strValue) {
		return $this->strIndent."		<input id=\"".$strId."\" name=\"".$strId."\" type=\"hidden\" value=\"".$strValue."\" />\r\n";
	}
	
	function TextArea($strId, $strLabel, $strValue, $arrOtherAttributes = array(), $intCols = 23, $intRows = 3, $intTabIndex = NULL){
		$strTabIndex = $this->_TabIndex($intTabIndex);
		$strValue = $this->_Value($strId, $strValue);
		$strOtherAttributes = $this->_OtherAttributes($arrOtherAttributes);
		$strClassName = $this->_ClassName($strId);
		$strLocator = $this->_Locator($strId, $strClassName);
		$strField  = "<textarea id=\"".$strId."\" name=\"".$strId."\" class=\"".$strClassName."\"".$strOtherAttributes.$strTabIndex.$strLocator." rows=\"$intRows\" cols=\"$intCols\">".$strValue."</textarea>";
		$blnWithLabel = ($strLabel == "") ? false : true;
 		return $this->_FormHtmlElement($strLabel, $strField, $strId, $blnWithLabel);
	}
	
	function SubmitButton($strId, $strCaption, $arrOtherAttributes = array(), $blnWithLabel = false, $strClassName = "STsubmit", $intTabIndex = NULL){
		$strTabIndex = $this->_TabIndex($intTabIndex);
		$strLocator = $this->_Locator($strId, $strClassName);
		$strOtherAttributes = $this->_OtherAttributes($arrOtherAttributes);
		$strField = "<input id=\"".$strId."\" type=\"submit\" value=\"".$strCaption."\" class=\"".$strClassName."\"".$strTabIndex.$strLocator.$strOtherAttributes." />";
		return $this->_FormHtmlElement("",$strField,$strId, $blnWithLabel);
	}
	
	function ResetButton($strId, $strCaption, $arrOtherAttributes = array(), $blnWithLabel = false, $strClassName = "STsubmit", $intTabIndex = NULL){
		$strTabIndex = $this->_TabIndex($intTabIndex);
		$strLocator = $this->_Locator($strId, $strClassName);
		$strOtherAttributes = $this->_OtherAttributes($arrOtherAttributes);
		$strField = "<input id=\"".$strId."\" type=\"reset\" value=\"".$strCaption."\" class=\"".$strClassName."\"".$strTabIndex.$strLocator.$strOtherAttributes." />";
		return $this->_FormHtmlElement("", $strField, $strId,$blnWithLabel);
	}
	
	
	function StripSlashes($strString){
		if (get_magic_quotes_gpc()) {
			return stripslashes($strString);
		} else {
			return $strString;
		}
	}
	
	function ErrMsgs($styleName = "STerrmsgs"){
		if (!$this->IsValid()) {
			$strErrMsgs = $this->strIndent."<ul class=\"$styleName\">\r\n" ;
			foreach ($this->arrErrMsgs as $strMsg) {
				$strErrMsgs .= $this->strIndent."	<li>".$strMsg."</li>\r\n";	
			}
			$strErrMsgs .=$this->strIndent."</ul>\r\n";
			return $strErrMsgs;
		}
		return false;
	}
	
	function InfMsgs($styleName = "STinfmsgs"){
		if (!$this->IsHavmsg()){
			$strInfMsgs = $this->strIndent."<ul class=\"$styleName\">\r\n";
			foreach ($this->arrInfMsgs as $strMsg)
				$strInfMsgs .= $this->strIndent."  <li>".$strMsg."</li>\r\n";
			$strInfMsgs .= $this->strIndent."</ul>\r\n";
			return $strInfMsgs;
		}
		return false;
	}
	
	function GetJs(){
		$strJsLib  = "<script type=\"text/javascript\">\r\n";
		$strJsLib .= "function ChangeClassName(strMenuId, strClassName)\r\n";
		$strJsLib .= "	{\r\n";
		$strJsLib .= "	var e;\r\n";
		$strJsLib .= "	e = document.getElementById(strMenuId);\r\n";
		$strJsLib .= "	e.className = strClassName;\r\n";
		$strJsLib .= "	}\r\n";
		$strJsLib .= "</script>\r\n";
		return $strJsLib;
	}
	
	function SetError($strKey, $strValue){
		$this->arrErrMsgs[$strKey] = $strValue;
		return true;
	}

	function SetInfor($strKey, $strValue){
		$this->arrInfMsgs[$strKey] = $strValue;
		return true;
	}

	function SetIndent($strIndent){
		$this->strIndent = $strIndent;
		return true;
	}

	function UseLocator(){
		$this->blnLocator = true;
		return true;
	}

	function IsPost(){
		if (strtoupper($_SERVER['REQUEST_METHOD']) == "POST")
			return true;
		else
			return false;
	}

	function IsValid(){
		if (count($this->arrErrMsgs) == 0)
			return true;
		else
			return false;
	}

	function IsHavmsg(){
		if (count($this->arrInfMsgs) == 0)
			return true;
		else
			return false;
	}

	function GetCSS(){
		$strCSS = "
.STlabel
	{
	text-align:right;
	padding-right:5px;
	width:100px;
	float:left;
	}

.STfield
	{
	width:100px;
	float:left;
	}

.STinput, .STselect, .STtextarea 
	{
	border:1px solid #666666;
	padding:5px;
	font:bold 16px \"Courier New\";
	color:#666666;
	}

.STnormal
	{
	background:#fff;
	border-color:#006699;
	}

.STerror
	{
	background:#FFF0F0;
	border-color:#cc0000;
	}

.STselected
	{
	background:#E4F5FA;
	border-color:#006699;
	}

.STfield
	{
	background:#E4F5FA;
	border-color:#006699;
	width:300px;
	}

.STsubmit
	{
	background:#fff;
	border-color:#006699;
	}

.STclear
	{
	clear:both;
	}

.STerrmsgs
	{
	margin:0;
	padding:10px 25px 10px 25px;
	width:470px;
	background:#FFF0F0;
	border:1px solid #cc0000;
	color:#cc0000;
	}
.STinfmsgs
	{
	margin:0;
	padding:10px 25px 10px 25px;
	width:470px;
	background:#F0FFF0;
	border:1px solid #00cc00;
	color:#00cc00;
	}

.STerrmsgs2
	{
	margin:0;
	padding:10px 25px 10px 25px;
	width:130px;
	background:#FFF0F0;
	border:1px solid #cc0000;
	color:#cc0000;
	}
.STinfmsgs2
	{
	margin:0;
	padding:10px 25px 10px 25px;
	width:130px;
	background:#F0FFF0;
	border:1px solid #00cc00;
	color:#00cc00;
	}

.STtable
	{
		
	}";
	print $strCSS;
	}
	
	// <ValidationMethods>
	function ChEmptiness($strKey, $strMessage){
		if (empty($this->arrPostData[$strKey]))
			return $this->_SetError($strKey, $strMessage);
		else
			return true;
	}

	function ChEmailAddr($strKey, $strMessage){
		if (preg_match("/^[a-z0-9_\\.\\-]+@+[a-z0-9_\\.\\-]+(\\.[a-z]{2,4})$/", $this->arrPostData[$strKey]) == 0)
			return $this->_SetError($strKey, $strMessage);
		else
			return true;
	}

	function ChMaxLength($strKey, $strMessage, $intMaxLength){
		if (strlen($this->arrPostData[$strKey]) > $intMaxLength)
			return $this->_SetError($strKey, $strMessage);
		else
			return true;
	}

	function ChMinLength($strKey, $strMessage, $intMaxLength){
		if (strlen($this->arrPostData[$strKey]) < $intMaxLength)
			return $this->_SetError($strKey, $strMessage);
		else
			return true;
	}

	function ChValidChrs($strKey, $strMessage, $strChrs){
		if (!$this->_ValidateChars($this->arrPostData[$strKey], $strChrs))
			return $this->_SetError($strKey, $strMessage);
		else
			return true;
	}

	function ChWithRegex($strKey, $strMessage, $strPattern){
		if (preg_match($strPattern, $this->arrPostData[$strKey]) === 0)
			return $this->_SetError($strKey, $strMessage);
		else
			return true;
	}	

	function ChCustomVal($strKey, $strMessage, $blnValidation){
		if (!$blnValidation)
			return $this->_SetError($strKey, $strMessage);
		else
			return true;
	}	

	function ChFileExist($strKey, $strMessage){
		if ($_FILES[$strKey]['name'] == "")
			return $this->_SetError($strKey, $strMessage);
		else
			return true;
	}

	function ChFileTypes($strKey, $strMessage, $arrFileTypes){
		$blnFlag = false;
		foreach($arrFileTypes as $strFileType)
			{
			if (strtoupper($_FILES[$strKey]['type']) == strtoupper($strFileType))
				$blnFlag = true;
			}
		if (!$blnFlag)
			return $this->_SetError($strKey, $strMessage);
		else
			return true;
	}

	function ChFileMaxSZ($strKey, $strMessage, $intMaxSize){
		if ($_FILES[$strKey]['size'] > $intMaxSize)
			return $this->_SetError($strKey, $strMessage);
		else
			return true;
	}
	// </Validation Methods>
	
	// <Private Methods>
	function _Locator($strId, $strClassName){
		$strLocator = (($this->blnLocator) ?  " onfocus=\"javascript:ChangeClassName('".$strId."', 'STselected');\" onblur=\"javascript:ChangeClassName('".$strId."', '".$strClassName."');\"" : "" );
		return $strLocator;
	}
	
	function _ClassName($strId){
		$strClassName = ((isset($this->arrErrMsgs[$strId])) ? "STerror" : "STnormal" );
		return $strClassName;
	}

	function _Value($strId, $strValue){
		$strValue = htmlspecialchars(((isset($this->arrPostData[$strId])) ? $this->StripSlashes($this->arrPostData[$strId]) : $strValue ));
		return $strValue;
	}

	function _TabIndex($intTabIndex){
		$strTabIndex = (($intTabIndex != NULL) ? " tabindex=\"".$intTabIndex."\"" : "" );
		return $strTabIndex;
	}

	function _OtherAttributes($arrOtherAttributes){
		$strOtherAttributes = "";
		while(list($strArrKey, $strArrValue) = each($arrOtherAttributes)){
			$strOtherAttributes .= " ".$strArrKey."=\"".$strArrValue."\"";
		}
		return $strOtherAttributes;
	}

	function _FormHtmlElement($strLabel, $strField, $strId, $blnWithLabel){
		if ($blnWithLabel){
			$strPlaceHolder .= $this->strIndent."        ".(($strId != NULL) ? "<label for=\"".$strId."\">" : "" ).$strLabel.(($strId != NULL) ? "</label>" : "" )."\r\n";
			$strPlaceHolder .= $this->strIndent."        ".$strField."\r\n";
		}
		else{
			$strPlaceHolder .= $this->strIndent."        ".$strField."\r\n";
		}
		return $strPlaceHolder;
	}

	function _ValidateChars($strVal, $strAllowdChars){
		for ($i=0; $i<strlen($strVal); $i++){
			if (strpos($strAllowdChars, substr($strVal, $i, 1)) === false){
				return false;
			}	
		}
		return true;
	}

	function _SetError($strKey, $strMessage){
		if (!isset($this->arrErrMsgs[$strKey])){
			$this->SetError($strKey, $strMessage);
		}	
		return false;
	}
		// </Private Methods>
	}
?>